﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using IBNet.UtilsDataStructuresAndExtensions;

namespace IBNet.DayTrade
{
   /// <summary>
   /// A storage class for trading strategies.
   /// </summary>
   public static class TradingStrategy
   {
      static Random random = new Random();

      public readonly static Func<PriceBarsAndIndicatorValues, StrategyResult>
      TestStrategy = new Func<PriceBarsAndIndicatorValues, StrategyResult>(
         (PriceBarsAndIndicatorValues priceBarsAndIndicatorValues) =>
         {
            if (random.NextDouble() < 0.50)
            {
               return StrategyResult.Long;
            }
            else
            {
               return StrategyResult.Short;
            }
         });

      public readonly static Func<PriceBarsAndIndicatorValues, StrategyResult>
      MainStrategy = new Func<PriceBarsAndIndicatorValues, StrategyResult>(
         (PriceBarsAndIndicatorValues priceBarsAndIndicatorValues) =>
         {
            ReadOnlyFixedLengthCollection<decimal?> sma200 = priceBarsAndIndicatorValues["sma200"];

            // SMA 200 takes longest to calculate of the indicators
            // so wait for it to be not null before calculating strategy.
            if (sma200[0] == null)
            {
               return StrategyResult.NoTrade;
            }

            ReadOnlyFixedLengthCollection<PriceBar> priceBars = priceBarsAndIndicatorValues.PriceBars;
            ReadOnlyFixedLengthCollection<decimal?> oneROC = priceBarsAndIndicatorValues["oneROC"];
            ReadOnlyFixedLengthCollection<decimal?> threeROC = priceBarsAndIndicatorValues["threeROC"];
            ReadOnlyFixedLengthCollection<decimal?> roc1i = priceBarsAndIndicatorValues["roc1i"];
            ReadOnlyFixedLengthCollection<decimal?> roc2i = priceBarsAndIndicatorValues["roc2i"];
            ReadOnlyFixedLengthCollection<decimal?> hlc3 = priceBarsAndIndicatorValues["hlc3"];
            ReadOnlyFixedLengthCollection<decimal?> sma10 = priceBarsAndIndicatorValues["sma10"];
            ReadOnlyFixedLengthCollection<decimal?> sma20 = priceBarsAndIndicatorValues["sma20"];
            ReadOnlyFixedLengthCollection<decimal?> sma50 = priceBarsAndIndicatorValues["sma50"];
            ReadOnlyFixedLengthCollection<decimal?> stochastic = priceBarsAndIndicatorValues["stochastic"];
            ReadOnlyFixedLengthCollection<decimal?> lowestLow0to9 = priceBarsAndIndicatorValues["lowestLow0to9"];
            ReadOnlyFixedLengthCollection<decimal?> lowestLow0to19 = priceBarsAndIndicatorValues["lowestLow0to19"];
            ReadOnlyFixedLengthCollection<decimal?> lowestLow0to29 = priceBarsAndIndicatorValues["lowestLow0to29"];
            ReadOnlyFixedLengthCollection<decimal?> highestHigh0to9 = priceBarsAndIndicatorValues["highestHigh0to9"];
            ReadOnlyFixedLengthCollection<decimal?> highestHigh0to19 = priceBarsAndIndicatorValues["highestHigh0to19"];
            ReadOnlyFixedLengthCollection<decimal?> highestHigh0to29 = priceBarsAndIndicatorValues["highestHigh0to29"];

            // Million dollar trading strategy omitted

            // A moving average crossover strategy will be included with the next version (pre-alpha 0.4)

            return StrategyResult.Short;
         });
   }

}